﻿using System;
using System.Collections.Generic;
using System.Text;
using GeneratorLibrary.Client.Base;
using System.Data;

namespace GeneratorLibrary.Client.iSeries {
    public class iSeriesFactory : IClientFactory {
        #region IClientFactory Members

        public string GetDefaultDACName(string server, string db) {
            return "iSeries" + server + "DAC";
        }

        public BaseDAC NewClassGenDAC(string connectionString, string schema) {
            return new iSeriesDAC(connectionString, schema);
        }

        public ClassBase NewDataClass(DataTable dt, string table, string classNamespace, string dacNamespace, string dacName) {
            return new iSeriesOLEDataClass(dt, table, classNamespace, dacNamespace, dacName);
        }
        public string getEmpeddedResourceOutputDAC_Path() {
            return "GeneratorLibrary.ClassGeneratorLibrary.iSeries.output_DAC.txt";
        }
        public string GetConnectionString(string server, string schema) {
            return "Provider=IBMDA400;Data Source=" + server + ";Default Collection=QSYS2;";
        }

        public string GetConnectionString(string server, string schema, string user, string password) {
            return "Provider=IBMDA400;Data Source=" + server + ";Default Collection=QSYS2;User ID=" + user + ";Password=" + password + ";";
        }

        public string CatalogSchema {
            get { return "QSYS2"; }
        }
        #endregion

        public List<CrudTableBase> GetCrudTables(DataTable dt) {
            List<CrudTableBase> tables = new List<CrudTableBase>();
            foreach (DataRow dr in dt.Rows) {
                CrudTableBase table;
                if (tables.Count == 0 || tables[tables.Count - 1].TableName != dr["TableName"].ToString()) {
                    table = new iSeriesCrudTable();
                    table.TableName = dr["TableName"].ToString();
                    tables.Add(table);
                } else {
                    table = tables[tables.Count - 1];
                }
                iSeriesCrudColumn column = new iSeriesCrudColumn(dr);
                table.Columns.Add((CrudColumnBase)column);
            }
            return tables;
        }
        public bool AllowsRunningScripts {
            get { return false; }
        }
        public bool ConnectionSupportsCreateProcs { 
            get { return false; } 
        }
        public string GetGOStatement() {
            return "";
        }
    }
}
